package com.tony.demo.mvp.model;

import android.content.Context;

import com.puhui.lib.http.HttpUtils;
import com.puhui.lib.http.retrofit.RequestResponse;
import com.puhui.lib.utils.JsonUtils;
import com.puhui.lib.utils.PHConstant;
import com.tony.demo.bean.AdvertisementInfo;
import com.tony.demo.constants.API_URL;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright:
 * Created by TangJian on 2019/4/20.
 * Description:
 * Modified:
 */

public class HomeModel extends BaseModel {

    public void getBannerData(final Context context, final ModelCallBack<List<AdvertisementInfo>> modelCallBack) {
        HttpUtils.newInstance().post(context, API_URL.ADV_GET, new RequestResponse<String>() {
            @Override
            public void onSuccess(String code, String message, String result) {
                List<AdvertisementInfo> infoList = new ArrayList<>(3);

                if (PHConstant.HTTP_STATUS.SUCCESS.equals(code)) {
                    JSONArray data = JsonUtils.getJSONArray(result);
                    if (data != null && data.length() > 0) {
                        int length = data.length();
                        for (int i = 0; i < length; i++) {
                            AdvertisementInfo info = new AdvertisementInfo(JsonUtils.getJsonObject(data, i));
                            infoList.add(info);
                        }
                    }
                }

                modelCallBack.onSuccess(infoList);
//                EventBus.getDefault().post(new EventGetBanner(infoList));
            }

            @Override
            public void onFailure(Throwable t) {
                super.onFailure(t);
//                EventBus.getDefault().post(new EventGetBanner(null));
                modelCallBack.onFailure(null);
            }
        });
    }
}
